package com.example.assetmanagement.controller;

import com.example.assetmanagement.common.ApiResponse;
import com.example.assetmanagement.common.PageRequest;
import com.example.assetmanagement.common.PageResponse;
import com.example.assetmanagement.domain.User;
import com.example.assetmanagement.service.UserService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @GetMapping("/list")
    public ApiResponse<PageResponse<User>> list(@Valid PageRequest pageRequest,
                                                @RequestParam(required = false) String username,
                                                @RequestParam(required = false) String fullName,
                                                @RequestParam(required = false) Integer status) {
        return ApiResponse.success(userService.list(pageRequest, username, fullName, status));
    }

    @GetMapping("/all")
    public ApiResponse<List<User>> all() {
        return ApiResponse.success(userService.getAll());
    }

    @GetMapping("/{id}")
    public ApiResponse<User> detail(@PathVariable Long id) {
        return ApiResponse.success(userService.getById(id));
    }

    @PostMapping
    public ApiResponse<?> add(@RequestBody @Valid User user) {
        userService.add(user);
        return ApiResponse.success(null);
    }

    @PutMapping("/{id}")
    public ApiResponse<?> update(@PathVariable Long id, @RequestBody @Valid User user) {
        userService.update(id, user);
        return ApiResponse.success(null);
    }

    @DeleteMapping("/{id}")
    public ApiResponse<?> delete(@PathVariable Long id) {
        userService.delete(id);
        return ApiResponse.success(null);
    }

    @DeleteMapping("/batch")
    public ApiResponse<?> batchDelete(@RequestBody List<Long> ids) {
        userService.batchDelete(ids);
        return ApiResponse.success(null);
    }

    @PutMapping("/{id}/status")
    public ApiResponse<?> updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        userService.updateStatus(id, status);
        return ApiResponse.success(null);
    }

    @PutMapping("/{id}/password")
    public ApiResponse<?> resetPassword(@PathVariable Long id) {
        userService.resetPassword(id);
        return ApiResponse.success(null);
    }
} 